<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Http\Admin\Logistics\Controllers;

use App\Eloquent\Ygt\Logistics;
use App\Engine\Func;
use Framework\BaseClass\Http\Admin\Controller;

class IndexController extends Controller
{
    public function index(){}
    public function lists()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = ['company_id'=>$companyId];
            $lists          = Logistics::getList($where,'',$pageSize,$offset)->toArray();
            $count          = Logistics::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $result         = json_encode($data);
            return $result;
        } else {
            return $this->view('lists');
        }
    }
    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $data               = request()->all();
        $isSubmit           = isset($data['is_submit']) ? $data['is_submit'] : 0;
        $id                 = request('id',0);
        if($isSubmit){
            unset($data['is_submit']);
            $data['company_id']     = $companyId;
            $result                   = Logistics::edit($data);
            if( $result ){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail( '失败' );
            }
        } else {
            if($id){
                $where              = ['id'=>$id];
                $info               = Logistics::getInfo($where);
                $logoImg            = Func::getImgUrlById($info->logo);
            }else{
                $info               = Logistics::getObject();
                $logoImg            = '';
            }
            $info->logo_img    = $logoImg;
            return $this->view('edit',compact('info'));
        }
    }
    public function delete(){}
}